package com.stylefeng.guns.rest.modular.wz.api.v3;

import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.PfFileMapper;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.CreateUserSigImpl;
import com.stylefeng.guns.rest.modular.wz.service.IPfFileService;
import com.stylefeng.guns.rest.modular.wz.util.TencentPushUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: wz_dev_app_shop
 * @description:
 * @author: Chenchao
 * @create: 2018-11-20 15:46
 **/
@RestController
@RequestMapping("/wz/api/v3/chat/")
public class WzChatApi {
    @Autowired
    private CreateUserSigImpl createUserSig;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private TencentPushUtil tencentPushUtil;
    @Autowired
    private PfFileMapper pfFileMapper;
    @Autowired
    private IPfFileService fileService;
    //admin 的sig 过期时间10年
    private static String sig= Consts.adminSig;

    private static String registerUrl= "https://console.tim.qq.com/v4/im_open_login_svc/account_import?usersig="+sig+"&apn=1&identifier=admin&sdkappid=1400155242&contenttype=json";


    @PostMapping("getSig")
    public Res testSig() throws Exception {
        Long userCode=jwtTokenUtil.getUserCodeFromToken();
        String sig= createUserSig.getUserSig(userCode.toString());
        return Res.success(sig);
    }

    @PostMapping("register")
    public Res register(){
        Long userCode=jwtTokenUtil.getUserCodeFromToken();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("Identifier",userCode.toString());
        //{"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0}
        //{"ActionStatus":"FAIL","ErrorInfo":"Invalid parameter.","ErrorCode":70402}
        JSONObject result= tencentPushUtil.doPost(registerUrl,jsonObject);
        if("OK".equals(result.get("ActionStatus"))){
            return Res.success();
        }else {
            return Res.fail(result.get("ErrorInfo"));
        }
    }

}
